package cn.tth.test.game;

import cn.tth.test.game.annotations.DI;
import cn.tth.test.game.annotations.Mapper;
import cn.tth.test.game.annotations.MapperScan;
import cn.tth.test.game.annotations.Scan;
import cn.tth.test.game.bean.BeanDefinition;
import cn.tth.test.game.bean.BeanUtil;
import cn.tth.test.game.maps.MainCity;
import cn.tth.test.game.scan.AnnotationScan;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;

@Scan("cn.tth.test.game")
@MapperScan("cn.tth.test.game.db.mapper")
@Slf4j
public class Application {

    public static void main(String[] args) {
        Map<String, BeanDefinition> run = run(Application.class);
        BeanDefinition beanDefinition = run.get("mainCity");
        MainCity mainCity = (MainCity) beanDefinition.getBean();
        mainCity.main();
    }

    private static Map<String, BeanDefinition> run(Class<?> c) {
        Annotation[] declaredAnnotations = c.getDeclaredAnnotations();
        for (Annotation annotation : declaredAnnotations) {
            if(annotation instanceof Scan){
                String value = ((Scan) annotation).value();
                List<String> classNameList;
                try {
                    classNameList = AnnotationScan.getAllPathUrl(value);
                    return BeanUtil.createBean(classNameList);
                } catch (IOException | ClassNotFoundException e) {
                    log.warn("run error：",e);
                }
            }else if(annotation instanceof MapperScan){
                String value = ((MapperScan) annotation).value();
                List<String> classNameList;
                try {
                    classNameList = AnnotationScan.getAllPathUrl(value);
                    return BeanUtil.createBean(classNameList);
                } catch (IOException | ClassNotFoundException e) {
                    log.warn("run error：",e);
                }
            }
        }
        return null;
    }
}
